/**
 * Copyright (C) 2012 GreenSoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.neosystem.user;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.ManagedBean;
import javax.enterprise.context.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;
import javax.inject.Inject;
import javax.inject.Named;

import ar.com.greensoft.jeecommons.web.jsf.export.ColumnValueMapper;
import ar.com.greensoft.jeecommons.web.jsf.export.ExportColInfo;
import ar.com.greensoft.jeecommons.web.jsf.export.impl.ExportColInfoImpl;
import ar.com.greensoft.neosystem.dao.GenericDao;
import ar.com.greensoft.neosystem.dao.PersonaJuridicaDao;
import ar.com.greensoft.neosystem.model.Direccion;
import ar.com.greensoft.neosystem.model.PersonaJuridica;
import ar.com.greensoft.neosystem.model.references.TipoDocumento;
import ar.com.greensoft.neosystem.search.SearchFilter;
import ar.com.greensoft.neosystem.search.TextSearchFilter;

/**
 * @author Jorge E. Villaverde
 * @since 1.0
 */
@ManagedBean
@SessionScoped
@Named
public class PersonaJuridicaBean extends PersonaBean<PersonaJuridica> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3478606977660856105L;
	
	private static final String ENTITY_PREFIX = "personaJuridica";
	
    @Inject
    private PersonaJuridicaDao dao;
	
	@Override
	protected GenericDao<PersonaJuridica, Long> getDao() {
		return dao;
	}

	@Override
	protected PersonaJuridica newEntity() {
		PersonaJuridica entity = new PersonaJuridica();
		entity.setTipoDocumento(TipoDocumento.CUIT);
		entity.setDireccion(new Direccion());
		return entity;
	}

	@Override
	protected Long getEntityId(PersonaJuridica t) {
		return t.getId();
	}

	@Override
	protected String entityPrefix() {
		return ENTITY_PREFIX;
	}

	@Override
	protected SearchFilter createSearchFilter() {
		return new TextSearchFilter();
	}
	
	public void cuitChangeListener(AjaxBehaviorEvent event) {
		String cuit = getEntity().getCuit();
		PersonaJuridica cliente = dao.findByCuit(cuit);

		if(cliente != null){
			setEntity(cliente);
			setEditing(true);
			if(getEntity().getDireccion().getLocalidad() != null){
				this.provincia = getEntity().getDireccion().getLocalidad().getProvincia();
				this.pais = getEntity().getDireccion().getLocalidad().getProvincia().getPais();
			}
		}else{
			if(isEditing()){
				create();
				setEditing(false);
			}
		}
	}
	
	@Override
	protected String getEntityName(PersonaJuridica entity) {
		return entity.getNombre();
	}

	@Override
	protected PersonaJuridica findEntityByName(String entityName) {
		return dao.findByName(entityName);
	}

	@Override
	protected List<ExportColInfo> getExportColInfoList() {
		List<ExportColInfo> ret = new ArrayList<ExportColInfo>();
		ret.add(new ExportColInfoImpl(0, "personaJuridicaCUIT"));
		ret.add(new ExportColInfoImpl(1, "personaJuridicaRazonSolcia"));		
		return ret;
	}

	@Override
	protected ColumnValueMapper<PersonaJuridica> getColumnValueMapper() {
		return new ColumnValueMapper<PersonaJuridica>() {
			@Override
			public String getColumnValue(int columnNumber, PersonaJuridica value) {
				switch (columnNumber) {
				case 0:
					return value.getCuit();
				case 1:
					return value.getNombre();
				}
				return "";
			}
		};
	}
	
	@Override
	protected void beforeCreate() {
		super.beforeCreate();
		try {
			getEntity().setNumeroDocumento(Long.parseLong(getEntity().getCuit()));
		} catch (NumberFormatException e) {
		}
	}	
}
